import axios from 'axios'
import qs from  'qs'

// create an axios instancenpm install --save axios vue-axios
const http = axios.create({
    baseURL: 'http://localhost:8082/', // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 1000 * 120, // request timeout
    withCredentials: true,
})

// request interceptor
http.interceptors.request.use(
    request => {
        if (request.method==="post"){
            request.data = qs.stringify(request.data);
            request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        }
        let token = sessionStorage.getItem("token")
        if(token){
            request.headers.token = token;
        }
        return request
    }
)

// response interceptor
http.interceptors.response.use(
    response => {
        return response;
    }
)

export default http
